package com.qc.bus.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.qc.bus.entity.BusSales;
import com.qc.bus.service.BusSalesService;

import com.qc.bus.vo.SalesVo;
import com.qc.sys.common.ActiveUser;
import com.qc.sys.common.ResultObj;
import com.qc.sys.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (BusSales)表控制层
 *
 * @author AltmanQccc
 * @since 2020-09-10 23:27:02
 */
@RestController
@RequestMapping("sales")
public class BusSalesController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private BusSalesService busSalesService;

    /**
     * 查询并展示数据
     * */
    @GetMapping("loadAllSales")
    public Object loadAllSales(SalesVo salesVo){
        return this.busSalesService.queryAllSales(salesVo);
    }

    /**
     * 添加销售信息
     * */
    @PostMapping("addSales")
    public ResultObj addSales(BusSales sales){
        try {
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
            SysUser user = activeUser.getUser();
            sales.setSalestime(new Date());
            sales.setOperateperson(user.getName());
            this.busSalesService.InsertSales(sales);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改销售信息
     * */
    @PostMapping("updateSales")
    public ResultObj updateSales(BusSales sales){
        try {
            this.busSalesService.UpdateSales(sales);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除销售信息
     * */
    @PostMapping("deleteSales")
    public ResultObj deleteSales(Integer id){
        try {
            this.busSalesService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}